VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "DAOUsuario"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
Private mvarConnection As utilConexion 'local copy

Public Property Set Connection(ByVal vdata As utilConexion)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.Connection = Form1
    Set mvarConnection = vdata
End Property


Public Function eliminar(bean As bUsuario) As Integer
On Error GoTo hnderr
Dim sql As String
sql = "DELETE FROM usuario WHERE snombre= '" + CStr(bean.sNombre) + "'"
mvarConnection.Connection.Execute sql
eliminar = UtilConstantes.OPT_DATOS_ELIMINADOS

hnderr:
If Err.Number = 0 Then Exit Function
eliminar = UtilConstantes.OPT_ERROR_ENCONTRADO
Err.Clear
End Function



Public Function guardarNuevo(bean As bUsuario) As Integer
On Error GoTo hnderr

Dim sql As String
sql = "INSERT INTO usuario(snombre, sclave, nestado, nperfil) "
sql = sql + " VALUES ('" + bean.sNombre + "','"
sql = sql + bean.sClave + "',"
sql = sql + CStr(bean.bEstado.nCodigo) + "," + CStr(bean.bperfil.nCodigo) + ")"
mvarConnection.Connection.Execute sql
guardarNuevo = UtilConstantes.OPT_DATOS_GUARDADOS
hnderr:
If Err.Number = 0 Then Exit Function
guardarNuevo = UtilConstantes.OPT_ERROR_ENCONTRADO
Err.Clear
End Function

Public Function actualizar(bean As bUsuario) As Integer
On Error GoTo hnderr

Dim sql As String
sql = "UPDATE usuario SET  sclave='" + bean.sClave + "', "
sql = sql + " nestado =" + CStr(bean.bEstado.nCodigo) + ", "
sql = sql + " nperfil =" + CStr(bean.bperfil.nCodigo) + " WHERE snombre = '" + bean.sNombre + "'"
mvarConnection.Connection.Execute sql
actualizar = UtilConstantes.OPT_DATOS_GUARDADOS
hnderr:
If Err.Number = 0 Then Exit Function
actualizar = UtilConstantes.OPT_ERROR_ENCONTRADO
Err.Clear
End Function

Public Function login(ByRef bean As bUsuario) As bUsuario
On Error GoTo hnderr

Dim sql As String
Dim rst As New Recordset
Dim record As bUsuario
sql = "SELECT sNombre, sNombre,  "
sql = sql + " estado.nCodigo as nCodEstado, estado.sDescripcion as sEstado,"
sql = sql + " perfil.nCodigo as nCodPerfil , perfil.sDescripcion as sPerfil "
sql = sql + " FROM estado INNER JOIN (perfil INNER JOIN usuario "
sql = sql + " ON perfil.nCodigo = usuario.nPerfil) ON estado.nCodigo = usuario.nEstado "
sql = sql + " WHERE sNombre = '" + bean.sNombre + "' and sClave ='" + bean.sClave + "'"
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly
If Not rst.EOF Then
    Set record = New bUsuario
    If Not IsNull(rst.Fields("sNombre")) Then record.sNombre = rst.Fields("sNombre").value
    If Not IsNull(rst.Fields("nCodEstado")) Then record.bEstado.nCodigo = rst.Fields("nCodEstado").value
    If Not IsNull(rst.Fields("sEstado")) Then record.bEstado.sDescripcion = rst.Fields("sEstado").value
    If Not IsNull(rst.Fields("nCodperfil")) Then record.bperfil.nCodigo = rst.Fields("nCodPerfil").value
    If Not IsNull(rst.Fields("sPerfil")) Then record.bperfil.sDescripcion = rst.Fields("sPerfil").value
End If
Set login = record
hnderr:
If Err.Number = 0 Then Exit Function
login = UtilConstantes.OPT_ERROR_ENCONTRADO
Err.Clear

End Function



Public Function getLista(bean As bUsuario) As Collection
Dim sql As String
Dim rst As New Recordset
Dim list As New Collection
Dim record As New bUsuario

sql = "SELECT snombre , nperfil , sperfil , nestado , sestado  "
sql = sql + " from proc_usuario_select ('" + bean.sNombre + "',"
sql = sql + CStr(bean.bperfil.nCodigo) + "," + CStr(bean.bEstado.nCodigo) + ")"
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly

While Not rst.EOF
    Set record = New bUsuario
    If Not IsNull(rst.Fields("snombre")) Then record.sNombre = rst.Fields("snombre").value
    If Not IsNull(rst.Fields("nperfil")) Then record.bperfil.nCodigo = rst.Fields("nperfil").value
    If Not IsNull(rst.Fields("sperfil")) Then record.bperfil.sDescripcion = rst.Fields("sperfil").value
    If Not IsNull(rst.Fields("nestado")) Then record.bEstado.nCodigo = rst.Fields("nestado").value
    If Not IsNull(rst.Fields("sestado")) Then record.bEstado.sDescripcion = rst.Fields("sestado").value
    rst.MoveNext
    list.Add record
Wend
Set getLista = list
End Function



Public Function getUsuario(bean As bUsuario) As bUsuario
Dim sql As String
Dim rst As New Recordset
Dim record As New bUsuario

sql = "SELECT sCodigo, sNombre, sClave,  estado.nCodigo as nCodEstado, estado.sDescripcion as sEstado,"
sql = sql + " dFecExpiracion , perfil.nCodigo as nCodPerfil , perfil.sDescripcion as sPerfil "
sql = sql + " FROM estado INNER JOIN (perfil INNER JOIN usuario "
sql = sql + " ON perfil.nCodigo = usuario.nPerfil) ON estado.nCodigo = usuario.nEstado "
sql = sql + " Where "
sql = sql + " sCodigo ='" + CStr(bean.sNombre) + "' "
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly

If Not rst.EOF Then
    Set record = New bUsuario
    If Not IsNull(rst.Fields("sCodigo")) Then record.sNombre = rst.Fields("sCodigo").value
    
    If Not IsNull(rst.Fields("sClave")) Then record.sClave = rst.Fields("sClave").value
    If Not IsNull(rst.Fields("nCodEstado")) Then record.bEstado.nCodigo = rst.Fields("nCodEstado").value
    If Not IsNull(rst.Fields("sEstado")) Then record.bEstado.sDescripcion = rst.Fields("sEstado").value
    If Not IsNull(rst.Fields("nCodperfil")) Then record.bperfil.nCodigo = rst.Fields("nCodPerfil").value
    If Not IsNull(rst.Fields("sPerfil")) Then record.bperfil.sDescripcion = rst.Fields("sPerfil").value
End If
Set getUsuario = record
End Function



